# -*- coding: utf-8 -*-
from scrapy.spiders import CrawlSpider
from scrapy.http import Request
from lxml import etree
from scrapy.selector import Selector
from novahq_maps.items import NovahqMapsItem
import re
import requests

class DfbhdSpider(CrawlSpider):
    name = "dfbhd"
    start_urls = [
        'http://www.novahq.net/?show=maps&cmd=view_maps&game=dfbhd&type=all&order=&search=&query=&start=0&limit=40'
    ]
    url = 'http://www.novahq.net/'

    def parse(self, response):
        item = NovahqMapsItem()
        htmlcode = response.body
        tdbody = re.search('<td.*?class="regbody">(.*?)</td>', htmlcode, re.S).group(1)
        # 去掉头部div
        head = re.findall('<div\salign="center">.*?</div>', tdbody, re.S)[0]
        tdbody = tdbody.replace(head, '')
        # 去掉尾部div
        foot = re.findall('<div\salign="center">.*?</div>', tdbody, re.S)[0]
        tdbody = tdbody.replace(foot, '')
        # 分割\
        maplistsrc = re.split('<hr size="1" class="hr".*?>', tdbody)
        # 去掉头部无效字段
        maplistsrc[0:1] = []
        # 去掉尾部无效字段
        length = len(maplistsrc)
        maplistsrc[length-1:length] = []

        for eachMap in maplistsrc:
            selector = etree.HTML(eachMap)
            mapname = selector.xpath('//span[1]/a/b/text()')[0]

            author = selector.xpath('//span[1]/text()')
            author = author[0].replace(' | By ', '')

            gametype = selector.xpath('//span[3]/text()')
            gametype = gametype[0].replace(u'\xa0', 'type-end--')
            gametype = gametype.replace('&nbsp', 'type-end--')
            gametype = re.search('[|]\s+(.*?)\s+type-end--', gametype, re.S).group(1)

            download_link = selector.xpath('//span[1]/a/@href')
            download_link = download_link[0].replace('./', self.url)

            preview = selector.xpath('//span[3]/a/@onclick')[0]
            preview = preview.replace("hqPopUp('./", self.url)
            preview = preview.replace("','640','480')", '')
            imgpage = requests.get(preview)
            imgselc = etree.HTML(imgpage.text)
            tmpimgurl = imgselc.xpath('//td/@background')
            tmpimgurl[0] = tmpimgurl[0].replace('./', self.url)
            image_urls = tmpimgurl

            item['mapname'] = mapname.encode('utf8')
            item['author'] = author.encode('utf8')
            item['gametype'] = gametype.encode('utf8')
            item['download_link'] = download_link
            item['image_urls'] = image_urls

            yield item

        selector = Selector(response)
        nextLink = selector.xpath('//td[@class="regbody"]/div[1]/a[last()]/@href').extract()
        if nextLink:
            nextLink = nextLink[0].replace('./', '')
            yield Request(self.url + nextLink, callback=self.parse)